﻿var grid, cbProgress;

//初始化表格
function InitGrid() {
    var GOODSBATCHCODE = $.trim($("#txtGOODSBATCHCODE").attr("value"));
    var LOCATIONCODE = $.trim($("#txtLOCATIONCODE").attr("value"));
    var GOODSCODE = $.trim($("#txtGOODSCODE").attr("value"));
    var INVGOODSCODE = $.trim($("#txtINVGOODSCODE").attr("value"));
    var GOODSNAME = $.trim($("#txtGOODSNAME").attr("value"));
    var INVGOODSNAME = $.trim($("#txtINVGOODSNAME").attr("value"));
    grid = $('#tab_list').datagrid({
        name: 'tbl_data',
        url: '/LocationInventory/Query',
        title: '货位库存列表',
        queryParams: {
            GOODSBATCHCODE: GOODSBATCHCODE,
            LOCATIONCODE: LOCATIONCODE,
            GOODSCODE: GOODSCODE,
            INVGOODSCODE: INVGOODSCODE,
            GOODSNAME: GOODSNAME,
            INVGOODSNAME: INVGOODSNAME
        },
        //width: 815,
        height: 650,
        //border : false,
        //fit: false,
        fitColumns: false,
        //nowrap: true,
        rownumbers: true,
        //showFooter: true,
        //loadMsg: '正在加载信息...',
        //frozen: true,
        //sortName: "CODE", //排序设置
        singleSelect: true,
        idField: 'LOCATIONINVENTORYID',
        columns: [[
        //{ field: 'ck', checkbox: true },
                    { field: 'LOCATIONINVENTORYID', title: '货位库存ID', hidden: true },
                    { field: 'INVGOODSBATCHCODE', title: '商品批次号', align: 'center', sortable: true },
                    { field: 'LOCATIONCODE', title: '货位编码', align: 'center', sortable: true },
                    { field: 'GOODSCODE', title: '商品编码', align: 'center', sortable: true },
                    { field: 'GOODSNAME', title: '商品名称', align: 'center', sortable: true },
                    { field: 'INVGOODSCODE', title: '下架商品编码', align: 'center', sortable: true },
                    { field: 'INVGOODSNAME', title: '下架商品名称', align: 'center', sortable: true },
                    { field: 'INVUNITNAME', title: '单位', align: 'center', sortable: true },
                    { field: 'WAREHOUSENAME', title: '仓库', align: 'center', sortable: true },
                    { field: 'CELLNO', title: '库位', align: 'center', sortable: true },
                    { field: 'PRODUCTIONDATE', title: '生产日期', align: 'center', sortable: true },
                    { field: 'STORAGEDATE', title: '入库日期', align: 'center', sortable: true },
                    { field: 'EXPIRYDAYS', title: '保质天数', align: 'center', sortable: true },
                    { field: 'QTY', title: '库存数', align: 'center', sortable: true },
                    { field: 'GOODSBATCHCODE', title: '有效天数', align: 'center', sortable: true, formatter: formatvalidday },
                    { field: 'INVSTATUS', title: '库存状态', align: 'center', sortable: true }
        ]],
        pagination: true,
        pageSize: 20,
        pageNumber: 1,
        pageList: [10, 20, 30, 40, 50],
        rownumbers: true, //行号
        onSortColumn: function (sort, order) {
            grid.datagrid('reload');
        }
    });
    //清空选择
    grid.datagrid('clearSelections');
}


function formatvalidday(val, row, index) {
    if (val != null) {
        var PRODUCTIONDATE = row.PRODUCTIONDATE;
        var PRODUCTIONYEAR = PRODUCTIONDATE.substr(0, 1);
        var PRODUCTIONMONTH = PRODUCTIONDATE.substr(1, 2);
        var PRODUCTIONDAY = PRODUCTIONDATE.substr(3, 2);
        var date = new Date();
        var year = date.getFullYear().toString().substr(3, 1);
        var month = date.getMonth();
        var day = date.getDate();
        var pastdate = (year - PRODUCTIONYEAR) * 365 + (month + 1 - PRODUCTIONMONTH) * 30 + (day - PRODUCTIONDAY);
        var EXPIRYDAYS = row.EXPIRYDAYS;
        if (pastdate >= 0 && EXPIRYDAYS - pastdate > 0) {
            return EXPIRYDAYS - pastdate;
        }
        else {
            return 0;
        }
    }
}